package com.bsj.travel.monitor.file.log;

import com.bsj.writeFile.ReadFileDisk;
import com.bsj.writeFile.def.DataVo;
import com.bsj.writeFile.motion.DataReadHandle;
import com.bsj.writeFile.motion.DiskModel;
import com.bsj.writeFile.motion.FReadAction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.bsj.JedisClusterBSJ;

import java.util.LinkedList;
import java.util.List;

/**
 * @author yinhao
 * @version 1.0
 * @description 日志读
 * @date 2024/1/26
 */
@Slf4j
@Service
public class LogRead implements FReadAction {

    private DataReadHandle readHandle;

    //间隔时间数
    private int intervalTime = 30;

    @Autowired
    private JedisClusterBSJ jedisClusterBSJ;

    @Value("${file.log}")
    private String filePath;

    public void start(String name) {
        log.info("filePath:" + filePath);
        ReadFileDisk readFileData = new ReadFileDisk(jedisClusterBSJ, filePath);
        readFileData.settingModel(DiskModel.MONTH);
        readHandle = readFileData;
        readHandle.settingObjName(name);
    }

    @Override
    public List<String> readStringAll(String s, long l, long l1) {
        return null;
    }

    @Override
    public List<byte[]> readBytesAll(String s, long l, long l1) {
        return null;
    }

    @Override
    public List<DataVo> readVoAll(String id, long start, long end) {
        if (start > end) {
            return null;
        }
        List<DataVo> result = new LinkedList<>();
        long intervalTimeLong = intervalTime * 60 * 1000;
        long nowTime = System.currentTimeMillis();
        long startInterval = nowTime - start;
        long endInterval = nowTime - end;
        //取出缓存数据既可满足
        if (endInterval < intervalTimeLong && startInterval > intervalTimeLong) {
            List<DataVo> one = readHandle.readRedisVo(id, start, end);
            List<DataVo> two = readHandle.readVoAll(id, start, end);
            if (one != null && !one.isEmpty()) {
                result.addAll(one);
            }

            if (two != null && !two.isEmpty()) {
                result.addAll(two);
            }
        } else if (startInterval < intervalTimeLong) {
            result = readHandle.readRedisVo(id, start, end);
        } else if (startInterval > intervalTimeLong) {
            result = readHandle.readVoAll(id, start, end);
        }
        return result;
    }
}
